let hj = "https://study.duyiedu.com";
// 设置令牌
function setLocaltorage(str) {
    localStorage.setItem("authorization", str);
}
// 获取令牌
function getLocaltorage() {
    return localStorage.getItem("authorization");
}
// 删除令牌
function removeLocaltorage() {
    localStorage.removeItem("authorization");
}
// 注册
/**
 * 注册
 * @param {Object} 传入一个对象 对象的具有 loginId、nickname、loginPwd 这三个属性
 * @returns promise
 */
async function reg(userInfo) {
    userInfo = JSON.stringify(userInfo);
    let url = hj + "/api/user/reg";
    let a = await fetch(url, {
        method: "POST",
        headers: {
            "Content-Type": "application/json"
        },
        body: userInfo
    });
    return a.json();
}
// 登录
/**
 * 登录
 * @param {Object} loginInfo 传入一个对象 对象具有loginId、loginPwd
 * @returns promise
 */
async function login(loginInfo) {
    loginInfo = JSON.stringify(loginInfo);
    let url = hj + "/api/user/login";
    let data = fetch(url, {
        headers: {
            "Content-Type": "application/json"
        },
        method: "POST",
        body: loginInfo
    });
    let sj = await data;
    // console.log(sj)
    // console.log(sj.headers);
    for (const item of sj.headers) {
        // console.log(item)
        if (item[0] == "authorization") {
            setLocaltorage(item[1]);
        }
    }
    return sj.json();
}
// 验证账号
/**
 * 验证账号
 * @param {String} loginId 账号id
 * @returns promise
 */
async function exists(loginId) {
    let url = hj + "/api/user/exists?" + `loginId=${loginId}`;
    let sj = await fetch(url);
    return sj.json();
}
// 当前用户登录的信息
async function profile() {
    let lp = getLocaltorage();
    let url = hj + "/api/user/profile";
    return (await fetch(url, {
        headers: {
            "Authorization": `Bearer ${lp}`
        }
    })).json()
}
/**
 * 发送聊天消息
 * @param {String} content 传入一个字符串他的值为聊天信息
 * @returns promise
 */
async function sendChat(content) {
    let lp = getLocaltorage();
    let url = hj + "/api/chat";
    let sj = {
        "content": content
    }
    return (await fetch(url, {
        method: "POST",
        headers: {
            "Authorization": `Bearer ${lp}`,
            "Content-Type": "application/json"
        },
        body: JSON.stringify(sj)
    })).json()
}
/**
 * 根据本地存储获取聊天记录
 * @returns 返回promise
 */
async function getHistory() {
    let lp = getLocaltorage();
    let url = hj + "/api/chat/history";
    return (await fetch(url, {
        headers: {
            "Authorization": `Bearer ${lp}`
        }
    })).json()
}